<template>
  <div class="register-container">
    <h2>注册</h2>
    <form @submit.prevent="handleRegister">
      <div class="form-group">
        <label for="username">用户名</label>
        <input
            id="username"
            v-model="form.username"
            type="text"
            required
        >
      </div>
      <div class="form-group">
        <label for="password">密码</label>
        <input
            id="password"
            v-model="form.password"
            type="password"
            required
        >
      </div>
      <div class="form-group">
        <label for="confirmPassword">确认密码</label>
        <input
            id="confirmPassword"
            v-model="form.confirmPassword"
            type="password"
            required
        >
      </div>
      <button type="submit" :disabled="loading">
        {{ loading ? '注册中...' : '注册' }}
      </button>
      <p v-if="error" class="error">{{ error }}</p>
    </form>
    <p>已有账号？<router-link to="/login">立即登录</router-link></p>
  </div>
</template>

<script>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '../../store'
import { registerUser } from '../../api/mockData'

export default {
  setup() {
    const form = ref({
      username: '',
      password: '',
      confirmPassword: ''
    })
    const error = ref('')
    const loading = ref(false)
    const router = useRouter()
    const authStore = useAuthStore()

    const handleRegister = async () => {
      if (form.value.password !== form.value.confirmPassword) {
        error.value = '两次输入的密码不一致'
        return
      }

      try {
        loading.value = true
        error.value = ''
        const response = await registerUser({
          username: form.value.username,
          password: form.value.password
        })
        authStore.login(response.user)
        router.push('/products')
      } catch (err) {
        error.value = err.message
      } finally {
        loading.value = false
      }
    }

    return { form, error, loading, handleRegister }
  }
}
</script>

<style scoped>
.register-container {
  max-width: 400px;
  margin: 2rem auto;
  padding: 2.5rem;
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
}

.register-container h2 {
  text-align: center;
  margin-bottom: 1.5rem;
  color: #2c3e50;
}

.form-group {
  margin-bottom: 1.5rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: 500;
  color: #2c3e50;
}

.form-group input {
  width: 100%;
  padding: 0.75rem;
  font-size: 1rem;
  border: 1px solid #e0e0e0;
  border-radius: 6px;
  transition: all 0.3s;
}

.form-group input:focus {
  border-color: #42b983;
  box-shadow: 0 0 0 3px rgba(66, 185, 131, 0.2);
  outline: none;
}

button {
  width: 100%;
  padding: 0.75rem;
  font-size: 1rem;
  font-weight: 500;
  background: #42b983;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s;
}

button:hover {
  background: #3aa876;
  transform: translateY(-1px);
}

button:active {
  transform: translateY(0);
}

button:disabled {
  background: #e0e0e0;
  cursor: not-allowed;
  transform: none;
}

.error {
  display: block;
  padding: 0.75rem;
  margin-top: 1rem;
  background: #ffeeee;
  color: #ff4444;
  border-radius: 6px;
  text-align: center;
}

p {
  text-align: center;
  margin-top: 1.5rem;
  color: #7f8c8d;
}

a {
  color: #42b983;
  font-weight: 500;
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}
</style>
